import { createVNode } from "vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
import { Modal, message } from "ant-design-vue";

import { getQueryPageApi, updateStateApi } from "@/api/approval/approvalManagement";
export default function OperationHook(state: Base.GeneraDataType) {
  const { searchForm, tableData, dialogOpts, loading } = state;
  const getData = () => {
    const params = {
      ...getParams()
    };

    loading.value = true;
    return getQueryPageApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.total = Number(data.total);
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };

  const getParams = () => {
    const { typeList, state, beforePageNum, pageNum, pageSize } = searchForm;
    const params: any = {
      typeList: [],
      state,
      beforePageNum,
      pageNum,
      pageSize
    };
    if (typeList)
      params.typeList = typeList
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");

    return params;
  };

  const searchHandle = () => {
    searchForm.pageNum = 1;
    getData();
  };
  const editHandle = (row: CountryRegion.ListRows) => {
    dialogOpts.title = "编辑问题件类型";
    dialogOpts.width = "800px";
    dialogOpts.type = 2;
    dialogOpts.component = "AddProblem";
    dialogOpts.passData = row;
    dialogOpts.visible = true;
  };
  const addHandle = () => {
    dialogOpts.title = "新增问题件类型";
    dialogOpts.width = "800px";
    dialogOpts.type = 1;
    dialogOpts.component = "AddProblem";
    dialogOpts.visible = true;
  };
  const changeStatusHandle = (row: RoleAndPermission.ListRows) => {
    let tip = null;
    if (row.state === 1) {
      tip = "是否停用该问题件类型?";
    } else {
      tip = "是否启用该问题件类型?";
    }
    Modal.confirm({
      title: tip,
      icon: createVNode(ExclamationCircleOutlined),
      content: "",
      okText: "确定",
      okType: "danger",
      cancelText: "取消",
      onOk() {
        updateStateApi(row.id).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            getData();
          }
        });
      }
    });
  };

  return {
    searchHandle,
    getData,
    editHandle,
    addHandle,
    changeStatusHandle
  };
}
